package com.imc.daoImpl;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.imc.dao.CommonDao;

public abstract class CommonDaoImpl<T> implements CommonDao<T> {

	private Class<T> t;

	@SuppressWarnings("unchecked")
	public CommonDaoImpl() {
		t = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
				.getActualTypeArguments()[0];
	}

	private HibernateTemplate ht;

	public HibernateTemplate getHt() {
		return ht;
	}

	@Resource(name = "hibernateTemplate")
	public void setHt(HibernateTemplate ht) {
		this.ht = ht;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public T getElementById(int id) {
		return (T) ht.get(t.getName(), id);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public ArrayList<T> getElementByName(String name) {
		String sqlStr = "from "+t.getName()+" "+t.getSimpleName().toLowerCase()+"  where "+t.getSimpleName().toLowerCase()+".name=?";
		return ((ArrayList<T>) ht.find(sqlStr, name));
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public ArrayList<T> getElements() {
		String sqlStr = "from "+t.getName();
		return ((ArrayList<T>) ht.find(sqlStr));
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional
	public ArrayList<T> getElementsBySql(String sqlStr) {
		ArrayList<T> arrayList = new ArrayList<T>();
		try {
			arrayList = ((ArrayList<T>) ht.getSessionFactory().openSession()
					.createSQLQuery(sqlStr).addEntity(t.getName()).list());
		} catch (Exception e) {
			return null;
		}
		return arrayList;
	}

	@Override
	@Transactional
	public int getElementsSize() {
		int len = 0;
		try {
			len = DataAccessUtils.intResult(ht.find("from "+t.getName()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return len;
	}

	@Override
	@Transactional
	public int getElementsSizeBySql(String sqlStr) {
		int len = 0;
		try {
			len = Integer.parseInt(ht.getSessionFactory().openSession()
					.createSQLQuery(sqlStr).list().get(0).toString());
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}

		return len;
	}

	@Override
	@Transactional
	public boolean delElement(int id) {
		try {
			// 先加载特定的实例
			Object p = ht.load(t.getName(), new Integer(id));
			// 删除特定实例
			ht.delete(p);
		} catch (DataAccessException e) {
			return false;
		}
		return true;
	}

	@Override
	@Transactional
	public boolean addElement(T t) {
		try {
			ht.saveOrUpdate(t);
		} catch (DataAccessException e) {
			return false;
		}
		return true;
	}

	@Override
	@Transactional
	public boolean updateElement(T t) {
		try {
			ht.update(t);
		} catch (DataAccessException e) {
			return false;
		}
		return true;
	}

	@Override
	@Transactional
	public boolean delElement(T t) {
		try {
			ht.delete(t);
		} catch (DataAccessException e) {
			return false;
		}
		return true;
	}

}
